﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using Fuse8.ContentManagementFramework.Domain;
using Fuse8.DomainFramework.Domain;

namespace Fuse8.ContentManagementFramework.BizFacade.Services.Contents.Queryable
{
    public static class CmsIQueryableExtensions
    {
         public static IQueryable<GenericCmsEntity> JoinContent(this IQueryable<GenericCmsEntity> source, int contentTypeId, string primaryFieldName, string foreignFieldName, Expression<Func<object>> selector)
         {
             var method = ((MethodInfo) MethodBase.GetCurrentMethod());
             return
                 source.Provider.CreateQuery<GenericCmsEntity>(Expression.Call(null, method,
                                                                               new[]
                                                                                   {
                                                                                       source.Expression,
                                                                                       Expression.Constant(contentTypeId),
                                                                                       Expression.Constant(primaryFieldName),
                                                                                       Expression.Constant(foreignFieldName),
                                                                                       selector
                                                                                   }));
         }

         public static IQueryable<GenericCmsEntity> JoinWith<T>(this IQueryable<GenericCmsEntity> source, Expression<Func<T, object>> primamryKey, string foreignFieldName, Expression<Func<T, object>> selector)
             where T:IEntityObject
         {
             var method = ((MethodInfo) (MethodBase.GetCurrentMethod())).MakeGenericMethod(typeof (T));
             return
                 source.Provider.CreateQuery<GenericCmsEntity>(Expression.Call(null, method,
                                                                               new[]
                                                                                   {
                                                                                       source.Expression,
                                                                                       primamryKey,
                                                                                       Expression.Constant(foreignFieldName),
                                                                                       selector
                                                                                   }));
         }

         public static IQueryable<GenericCmsEntity> JoinWith(this IQueryable<GenericCmsEntity> source, string primamryKey, string foreignFieldName, Expression<Func<object>> selector, Type type)
         
         {
             var method = ((MethodInfo)MethodBase.GetCurrentMethod());
             return
                 source.Provider.CreateQuery<GenericCmsEntity>(Expression.Call(null, method,
                                                                               new[]
                                                                                   {
                                                                                       source.Expression,
                                                                                       Expression.Constant(primamryKey),
                                                                                       Expression.Constant(foreignFieldName),
                                                                                       selector,
                                                                                       Expression.Constant(type)
                                                                                   }));
         }

        public static IQueryable<GenericCmsEntity> SelectProperties(this IQueryable<GenericCmsEntity> source, params string[] names)
        {
            var method = ((MethodInfo)MethodBase.GetCurrentMethod());
            return source.Provider.CreateQuery<GenericCmsEntity>(Expression.Call(null, method, new[] {source.Expression, Expression.Constant(names)}));
        }
    }
}